package com.java.business.system.common.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.java.business.system.common.bean.TreeParam;
import com.java.business.system.common.service.CommonService;
import com.java.core.bean.LowerCaseKeyMap;
import com.java.core.bean.ResultBean;
import com.java.core.utils.RequestUtils;
import com.java.core.utils.ResponseUtils;

@RequestMapping("/system/common")
@Controller
public class CommonController {

	@Autowired
	private CommonService service;

	@RequestMapping("/queryForMap")
	public void queryForMap(HttpServletResponse response,
			HttpServletRequest request) {
		ResultBean rb = new ResultBean();
		try {
			LowerCaseKeyMap map = service.queryForMap(RequestUtils
					.getParams(request));
			rb.setData(map);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/queryForList")
	public void queryForList(HttpServletResponse response,
			HttpServletRequest request) {
		ResultBean rb = new ResultBean();
		try {
			List<LowerCaseKeyMap> list = service.queryForList(RequestUtils
					.getParams(request));
			rb.setData(list);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/queryBySql")
	public void queryBySql(HttpServletResponse response,
			HttpServletRequest request) {
		ResultBean rb = new ResultBean();
		try {
			List<LowerCaseKeyMap> list = service.queryBySql(RequestUtils
					.getParams(request));
			rb.setData(list);
			rb.setTotal(service.queryCount(RequestUtils.getParams(request)));
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/queryForTree")
	public void queryForTree(HttpServletResponse response,
			HttpServletRequest request, TreeParam params) {
		List<LowerCaseKeyMap> list = service.queryForTree(params);
		ResponseUtils.renderJson(response, JSON.toJSONString(list));
	}

	@RequestMapping("/queryDefault")
	public void queryDefault(HttpServletResponse response,
			HttpServletRequest request, TreeParam params) {
		ResultBean rb = new ResultBean();
		try {
			String text = service.queryDefault(params);
			rb.setData(text);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/exec")
	public void exec(HttpServletResponse response, HttpServletRequest request,
			String sql) {
		ResultBean rb = new ResultBean();
		try {
			Map<String, String> params = RequestUtils.getParams(request);
			service.exec(params);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("export2Excel")
	public void export2Excel(HttpServletResponse response,
			HttpServletRequest request, String sql) {
		ResultBean rb = new ResultBean();
		try {
			service.export2Excel(response, request);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
	}
}
